package com.example.myspringbootpj.controller;

import com.example.myspringbootpj.entity.Note;
import com.example.myspringbootpj.entity.NoteDTO;
import com.example.myspringbootpj.entity.Word;
import com.example.myspringbootpj.mapper.WordMapper;
import com.example.myspringbootpj.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.myspringbootpj.entity.User;
import com.example.myspringbootpj.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.MediaType;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping(value = "/user")
public class userController {
    private final UserMapper userMapper;
private final UserService userService;
    private final WordMapper wordMapper;

    @Autowired
    public userController(UserMapper userMapper, UserService userService, WordMapper wordMapper) {
        this.userMapper = userMapper;
        this.userService = userService;
        this.wordMapper = wordMapper;
    }
//用户登录
    // 处理用户登录请求，接收前端传来的包含用户编号和密码的JSON数据
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody User user , HttpSession session) {
        String userId = user.getUser_id();
        String password = user.getUser_password();

        if(userService.IsBan(userId,password)){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body("您已被封号，请联系管理员查看原因喵");
        }

        User foundUser = userMapper.selectByUserIdAndPassword(userId, password);

        // 如果用户不存在，返回错误信息
        if (foundUser == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body("用户名或密码错误捏");
        }

//保存用户id到session
        session.setAttribute("userID", foundUser.getUser_id());

        // 如果登录成功，返回用户信息
        return ResponseEntity.ok("登录成功( •̀ ω •́ )");

    }


    //增加酷币数量
    @PostMapping("/updateKUBI")
    public int updateKubi(@RequestParam("user_kubi") int user_kubi,HttpSession session) {

        String user_id = (String) session.getAttribute("userID");

        userMapper.updateUserKubi(user_id, user_kubi);
        return user_kubi;
    }

    //注销
    @PostMapping("/logout")
    public ResponseEntity<String> logout(HttpSession session) {
        session.invalidate();  // 销毁整个 Session
        return ResponseEntity.ok("已成功退出登录");
    }

    //写笔记
    @PostMapping("/addNote")
    public String InsertNote(@RequestParam("word_id") Integer word_id,@RequestParam("note") String note,HttpSession session) {
        String user_id = (String) session.getAttribute("userID");


//            Integer wordId = Integer.parseInt(String.valueOf(word_id));
            userMapper.addNote(user_id, word_id, note);
            return "笔记添加成功";

    }

    //看笔记
          @GetMapping("/getNotes")
        public List<NoteDTO> getNotes(HttpSession session) {
        String user_id = (String) session.getAttribute("userID");

        List<Word> words = userMapper.selectWordNote(user_id);

       List<NoteDTO> notes = words.stream().map(word -> {
           String note = userMapper.selectNoteByWordIdAndUserId(user_id, String.valueOf(word.getWord_id()));
           return new NoteDTO(String.valueOf(word.getWord_id()),word.getWord_text(),note);
       }).collect(Collectors.toList());
      return notes;
          }


}